package com.gxm.练习.反射;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author 郭训民
 * @description: TODO
 * @title: Demo
 * @projectName jsee
 * @date 2021/4/10 9:37
 */
public class Demo {

    @Test
    public void test01(){
        System.out.println(Demo.class.getPackage().getName());
        Map<String, String> map = new HashMap<>();
        map.put("1", "1");
        System.out.println(map.get(null));
    }

    @Test
    public void test02(){
        Boolean get = null;
        if (get) {
            System.out.println(get);
        } else  {
            System.out.println(get);
        }
    }

    @Test
    public void test03(){
        List<String> list = new ArrayList<>();
        setList(list);
        System.out.println(list);
    }

    private void setList (List<String> list) {
        List<String> list1 = new ArrayList<>();
        list1.add("ss");
        list = list1;
        System.out.println("setList:" + list);
    }

    @Test
    public void test1(){
        //初始化线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        List<Future<String>> list= new ArrayList<Future<String>>();//
        for (int i = 0; i <10 ; i++) {
            Callable threadTest = new ThreadTest(i);
            try {
                Future submit = executorService.submit(threadTest);
                list.add(submit);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //任务执行完成后关闭线程池
        executorService.shutdown();
        while(true){
            //线程池中线程是否全部执行完毕
            if(executorService.isTerminated()){
                list.stream().forEach(future -> {
                    try {
                        //打印结果
                        System.out.println(future.get().toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                });
                break;
            }
        }

    }

    public class ThreadTest implements Callable {
        private int count;

        public ThreadTest(Integer count){
            this.count = count;
        }
        @Override
        public String call() throws Exception {
            int a = 1/0;
            return "测试"+count;
        }
    }
}
